package com.ruoyi.caipiao.service.impl;

import java.util.*;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.caipiao.utils.Pl5NumUtil;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.caipiao.mapper.CaipiaoPl5MethodFourMapper;
import com.ruoyi.caipiao.domain.CaipiaoPl5MethodFour;
import com.ruoyi.caipiao.service.ICaipiaoPl5MethodFourService;

/**
 * 排列5方法4预测Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-08
 */
@Slf4j
@Service
public class CaipiaoPl5MethodFourServiceImpl extends ServiceImpl<CaipiaoPl5MethodFourMapper, CaipiaoPl5MethodFour> implements ICaipiaoPl5MethodFourService {
    @Autowired
    private CaipiaoPl5MethodFourMapper caipiaoPl5MethodFourMapper;

    @Override
    public CaipiaoPl5MethodFour generateCode(CaipiaoPl5MethodFour caipiaoPl5MethodTwo) {
        if (caipiaoPl5MethodTwo.getQs() == null) {
            throw new RuntimeException("请选择预测的期数!");
        }
        int initSize = 250;
        Set<String> nums = Pl5NumUtil.getNums(initSize);
        for (; ; ) {
            nums.retainAll(Pl5NumUtil.getNums(initSize));
            if (nums.size() <= 3) {
                log.info("方法4：{}，{}" , nums.size(), nums.toString());
                if (nums.size() == 3) {
                    return getCaipiaoPl5MethodThree(caipiaoPl5MethodTwo, new ArrayList<>(nums), 3);
                } else if (nums.size() == 2) {
                    return getCaipiaoPl5MethodThree(caipiaoPl5MethodTwo, new ArrayList<>(nums), 2);
                } else if (nums.size() == 1) {
                    return getCaipiaoPl5MethodThree(caipiaoPl5MethodTwo, new ArrayList<>(nums), 1);
                } else {
                    nums = Pl5NumUtil.getNums(initSize);
                }
            }
        }
    }

    private CaipiaoPl5MethodFour getCaipiaoPl5MethodThree(CaipiaoPl5MethodFour CaipiaoPl5MethodThree, List<String> nums, int size) {
        CaipiaoPl5MethodFour add = new CaipiaoPl5MethodFour();
        add.setId(IdUtil.getSnowflakeNextId());
        add.setCreateTime(new Date());
        add.setQs(CaipiaoPl5MethodThree.getQs());
        add.setNumSize(size);

        if (size == 3) {
            Iterator<String> iterator = nums.iterator();
            String code1 = nums.get(0);
            String code2 = nums.get(1);
            String code3 = nums.get(2);
            String p1 = code1.charAt(0) + "," + code2.charAt(0) + "," + code3.charAt(0);
            String p2 = code1.charAt(1) + "," + code2.charAt(1) + "," + code3.charAt(1);
            String p3 = code1.charAt(2) + "," + code2.charAt(2) + "," + code3.charAt(2);
            String p4 = code1.charAt(3) + "," + code2.charAt(3) + "," + code3.charAt(3);
            String p5 = code1.charAt(4) + "," + code2.charAt(4) + "," + code3.charAt(4);
            add.setPosition1(p1);
            add.setPosition2(p2);
            add.setPosition3(p3);
            add.setPosition4(p4);
            add.setPosition5(p5);
        } else if (size == 2) {
            Iterator<String> iterator = nums.iterator();
            String code1 = iterator.next();
            String code2 = iterator.next();
            String p1 = code1.charAt(0) + "," + code2.charAt(0);
            String p2 = code1.charAt(1) + "," + code2.charAt(1);
            String p3 = code1.charAt(2) + "," + code2.charAt(2);
            String p4 = code1.charAt(3) + "," + code2.charAt(3);
            String p5 = code1.charAt(4) + "," + code2.charAt(4);
            add.setPosition1(p1);
            add.setPosition2(p2);
            add.setPosition3(p3);
            add.setPosition4(p4);
            add.setPosition5(p5);
        } else {
            Iterator<String> iterator = nums.iterator();
            String code1 = iterator.next();
            String p1 = code1.substring(0, 1);
            String p2 = code1.substring(1, 2);
            String p3 = code1.substring(2, 3);
            String p4 = code1.substring(3, 4);
            String p5 = code1.substring(4, 5);
            add.setPosition1(p1);
            add.setPosition2(p2);
            add.setPosition3(p3);
            add.setPosition4(p4);
            add.setPosition5(p5);
        }

        return add;
    }

    @Override
    public List<CaipiaoPl5MethodFour> checkCode(CaipiaoPl5MethodFour CaipiaoPl5MethodThree) {
        List<CaipiaoPl5MethodFour> list = caipiaoPl5MethodFourMapper.listNoCheckNum(CaipiaoPl5MethodThree);
        if (!list.isEmpty()) {
            for (CaipiaoPl5MethodFour one : list) {
                if (one.getKjNum() != null) {
                    String kjNum = one.getKjNum();
                    boolean zj = true;
                    boolean zjPl3 = true;
                    if (!one.getPosition1().contains(kjNum.substring(0, 1))) {
                        zj = false;
                        zjPl3 = false;
                    }
                    if (!one.getPosition2().contains(kjNum.substring(1, 2))) {
                        zj = false;
                        zjPl3 = false;
                    }
                    if (!one.getPosition3().contains(kjNum.substring(2, 3))) {
                        zj = false;
                        zjPl3 = false;
                    }
                    if (!one.getPosition4().contains(kjNum.substring(3, 4))) {
                        zj = false;
                    }
                    if (!one.getPosition5().contains(kjNum.substring(4, 5))) {
                        zj = false;
                    }
                    if (zj) {
                        one.setRightPl5(1);
                    } else {
                        one.setRightPl5(2);
                    }

                    if (zjPl3) {
                        one.setRightPl3(1);
                    } else {
                        one.setRightPl3(2);
                    }
//                    caipiaoPl5MethodFourMapper.updateCaipiaoPl5MethodFour(one);
                }
            }
        }
        return list;
    }

    /**
     * 查询排列5方法4预测
     *
     * @param id 排列5方法4预测主键
     * @return 排列5方法4预测
     */
    @Override
    public CaipiaoPl5MethodFour selectCaipiaoPl5MethodFourById(Long id) {
        return caipiaoPl5MethodFourMapper.selectCaipiaoPl5MethodFourById(id);
    }

    /**
     * 查询排列5方法4预测列表
     *
     * @param caipiaoPl5MethodFour 排列5方法4预测
     * @return 排列5方法4预测
     */
    @Override
    public List<CaipiaoPl5MethodFour> selectCaipiaoPl5MethodFourList(CaipiaoPl5MethodFour caipiaoPl5MethodFour) {
        return caipiaoPl5MethodFourMapper.selectCaipiaoPl5MethodFourList(caipiaoPl5MethodFour);
    }

    /**
     * 新增排列5方法4预测
     *
     * @param caipiaoPl5MethodFour 排列5方法4预测
     * @return 结果
     */
    @Override
    public int insertCaipiaoPl5MethodFour(CaipiaoPl5MethodFour caipiaoPl5MethodFour) {
        caipiaoPl5MethodFour.setCreateTime(DateUtils.getNowDate());
        return caipiaoPl5MethodFourMapper.insertCaipiaoPl5MethodFour(caipiaoPl5MethodFour);
    }

    /**
     * 修改排列5方法4预测
     *
     * @param caipiaoPl5MethodFour 排列5方法4预测
     * @return 结果
     */
    @Override
    public int updateCaipiaoPl5MethodFour(CaipiaoPl5MethodFour caipiaoPl5MethodFour) {
        return caipiaoPl5MethodFourMapper.updateCaipiaoPl5MethodFour(caipiaoPl5MethodFour);
    }

    /**
     * 批量删除排列5方法4预测
     *
     * @param ids 需要删除的排列5方法4预测主键
     * @return 结果
     */
    @Override
    public int deleteCaipiaoPl5MethodFourByIds(Long[] ids) {
        return caipiaoPl5MethodFourMapper.deleteCaipiaoPl5MethodFourByIds(ids);
    }

    /**
     * 删除排列5方法4预测信息
     *
     * @param id 排列5方法4预测主键
     * @return 结果
     */
    @Override
    public int deleteCaipiaoPl5MethodFourById(Long id) {
        return caipiaoPl5MethodFourMapper.deleteCaipiaoPl5MethodFourById(id);
    }
}
